﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using log4net;
using System.Threading;
using DWH.Data;
using DWH.Utils;

namespace DWH.PP.Push
{
    public class PageImageDownloadWorker : PageAttachmentDownloadWorkerBase
    {
        private ILog logger;
        private static int WorkInstanceID = 1;       

        public PageImageDownloadWorker()
        {
            logger = LogManager.GetLogger(typeof(PageImageDownloadWorker));
        }

        public override void Initialize()
        {
            base.Initialize();            
        }

        public override void DoWork()
        {
            int thisWorkerInstanceID = WorkInstanceID++;

            logger.InfoFormat("Worker {0}-ID:{1} start to work", TaskName, thisWorkerInstanceID);

            while (true)
            {
                if (ProcessingCollection.Count == 0)
                {
                    logger.InfoFormat("items finished, Worker {0} try to add items", thisWorkerInstanceID);

                    AddProcessingItems();
                }

                PageAttachment item = null;

                try
                {
                    item = ProcessingCollection.Take();

                    logger.InfoFormat("Worker {0}-ID:{1} ,processing item: {2}", TaskName, thisWorkerInstanceID, item.PageAttachmentUrl);
                    ProcessPageAttachment(item, thisWorkerInstanceID);

                }
                catch (Exception ex)
                {
                    logger.ErrorFormat("Worker {0}-ID:{1} , Exception when processing item {2}, ex: {3}", TaskName, thisWorkerInstanceID, item.PageAttachmentUrl, ex.ToString());
                }

                //Thread.Sleep(1 * 1000);
                DWHHelper.SimulateUserClickInterval(1,false);
            }
        }

        public void AddProcessingItems()
        {
            //ProcessingCollection.Add("");
            List<PageAttachment> atts = null;

            while (atts == null || atts.Count == 0)
            {
                logger.InfoFormat("no new attachments in database, sleep for 5 minutes to continue");
                DWHHelper.SimulateUserClickInterval(5 * 60, false);    
            
                atts = DWHDBHelper.GetPageAttachmentByAttachmentType(DWHConstants.ATTACHMENT_TYPE_IMAGE);
            }

            foreach (PageAttachment att in atts)
            {
                ProcessingCollection.Add(att);
            }

            logger.InfoFormat("add PageAttachments to process, count:{0}",atts.Count);
        }

        //public bool IsMatch(PageAttachment item)
        //{
        //    return true;
        //}

        public void ProcessPageAttachment(PageAttachment att, int thisWorkerInstanceID)
        {
            try
            {                
                DWHHelper.DownloadWebPageImage(TaskName, att.StoredPath, att.PageAttachmentUrl);
                //DWHHelper.DownloadWebPageImage(TaskName, @"E:\test", att.PageAttachmentUrl);
                logger.InfoFormat("save pic:{0} to path:{1}",att.PageAttachmentUrl,att.StoredPath);

                DWHDBHelper.UpdatePageAttachmentDownloadStatus(att.ID, TaskName, DateTime.Now, PageAttachmentDownloadStatus.Downloaded);
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("Worker {0}-ID:{1} , Exception when saving item {2}, ex: {3}", TaskName, thisWorkerInstanceID, att.PageAttachmentUrl, ex.ToString());
            }
        }
    }
}
